package com.back.network.web.serviceImpl;

import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import com.back.network.web.dao.EmailInfoDao;
import com.back.network.web.entity.EmailInfo;
import com.back.network.web.pojo.MailPOJO;
import com.back.network.web.service.MailService;

@Service
public class MailServiceImpl implements MailService {
	@Autowired
	private JavaMailSender javaMailSender;
	@Autowired
	EmailInfoDao emailDao;
	@Value("${com.back.network.from}")
	String from;
	@Value("${com.back.network.domain}")
	String domain;

	@Override
	public void sendSimpleMail(MailPOJO mail) {
		try {
			SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
			// 邮件发送人
			simpleMailMessage.setFrom(from);
			// 邮件接收人
			simpleMailMessage.setTo(mail.getRecipient());
			// 邮件主题
			simpleMailMessage.setSubject(mail.getSubject());
			// 邮件内容
			simpleMailMessage.setText(mail.getContent());
			javaMailSender.send(simpleMailMessage);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void sendHTMLMail(MailPOJO mail) {
		MimeMessage mimeMailMessage = null;
		try {
			mimeMailMessage = javaMailSender.createMimeMessage();
			MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
			mimeMessageHelper.setFrom(from);
			mimeMessageHelper.setTo(mail.getRecipient());
			mimeMessageHelper.setSubject(mail.getSubject());
			mimeMessageHelper.setText(mail.getContent(), true);
			javaMailSender.send(mimeMailMessage);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void sendSignUpMail(String email, String code) {
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append(
				"<table width=\"800\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" bgcolor=\"#fff\" style=\"font-family:'Microsoft YaHei'\">");
		stringBuilder.append("<tbody><tr><td>");
		stringBuilder.append(
				"<table width=\"800\" border=\"0\" align=\"left\" cellpadding=\"0\" cellspacing=\"0\" bgcolor=\"#81d4fa\" height=\"48\" style=\"font-family:'Microsoft YaHei'\">");
		stringBuilder.append(
				"<tbody><tr><td width=\"74\" height=\"26\" border=\"0\" align=\"left\" valign=\"middle\" style=\"padding-left:20px\">");
		stringBuilder.append(
				"<font color=\"white\" size=\"6\">小鼹鼠</font></td> </tr> </tbody> </table> </td></tr><tr style=\"padding:28px 18px;display:inline-block;\">");
		stringBuilder.append(
				"<td><table width=\"763\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" style=\"font-family:'Microsoft YaHei'\"><tbody> <tr>");
		stringBuilder.append("<td>只差一步，即可完成小鼹鼠注册！</td> </tr> <tr style=\"margin-top:12px;display:block\">");
		stringBuilder.append("<td style=\"color: #666666;font-size: 14px;\">请将下方展示的六位验证数字输入进注册验证框，并完成注册。</td>");
		stringBuilder.append(
				"</tr> <tr style=\"margin:40px 0;display:block;\" align=\"center\"> <td style=\"border:1px solid #A4D3FB;\" width=\"238\" valign=\"middle\">");
		stringBuilder.append(
				"<span style=\"width:35%;display:inline-block;float:left;height:100%;padding: 10px 0;background:#A4D3FB;color: white;\">验证数字</span>");
		stringBuilder.append(
				"<span style=\"width:65%;display:inline-block;float:left;letter-spacing: 5px;height:100%;padding: 10px 0;color: #1C90F6;\">");
		stringBuilder.append(code);
		stringBuilder.append(
				"</span></tr> <tr> <td width=\"720\" height=\"14\" colspan=\"2\" style=\"padding:8px 0 28px;color:#999999;font-size:12px;font-family:'Microsoft YaHei'\">此为系统邮件请勿回复</td>");
		stringBuilder.append(
				"</tr> </tbody> </table> </td> </tr><tr style=\"background-color: #F4F4F4;color: #999999;height: 100px;line-height: 100px;padding-left:20px;font-size: 13px;display:block;\">");
		stringBuilder.append(
				"<td> Copyrights © "+domain +" <span style=\"border-bottom:1px dashed #ccc;z-index:1\" t=\"7\" onclick=\"return false;\" data=\"2019\">2019</span>. All rights reserved. </td> ");
		stringBuilder.append("</tr></tbody></table>");
		MailPOJO mail = new MailPOJO();
		mail.setRecipient(email);
		mail.setSubject("小鼹鼠注册邮箱验证");
		mail.setContent(stringBuilder.toString());
		sendHTMLMail(mail);
	}

	@Override
	public void sendResetPwdMail(String email, String code) {
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append(
				"<table width=\"800\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" bgcolor=\"#fff\" style=\"font-family:'Microsoft YaHei'\">");
		stringBuilder.append("<tbody><tr><td>");
		stringBuilder.append(
				"<table width=\"800\" border=\"0\" align=\"left\" cellpadding=\"0\" cellspacing=\"0\" bgcolor=\"#81d4fa\" height=\"48\" style=\"font-family:'Microsoft YaHei'\">");
		stringBuilder.append(
				"<tbody><tr><td width=\"74\" height=\"26\" border=\"0\" align=\"left\" valign=\"middle\" style=\"padding-left:20px\">");
		stringBuilder.append(
				"<font color=\"white\" size=\"6\">小鼹鼠</font></td> </tr> </tbody> </table> </td></tr><tr style=\"padding:28px 18px;display:inline-block;\">");
		stringBuilder.append(
				"<td><table width=\"763\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" style=\"font-family:'Microsoft YaHei'\"><tbody> <tr>");
		stringBuilder.append("<td></td> </tr> <tr style=\"margin-top:12px;display:block\">");
		stringBuilder.append("<td style=\"color: #666666;font-size: 14px;\">请将下方展示的六位验证数字输入进验证框，并完成重置。</td>");
		stringBuilder.append(
				"</tr> <tr style=\"margin:40px 0;display:block;\" align=\"center\"> <td style=\"border:1px solid #A4D3FB;\" width=\"238\" valign=\"middle\">");
		stringBuilder.append(
				"<span style=\"width:35%;display:inline-block;float:left;height:100%;padding: 10px 0;background:#A4D3FB;color: white;\">验证数字</span>");
		stringBuilder.append(
				"<span style=\"width:65%;display:inline-block;float:left;letter-spacing: 5px;height:100%;padding: 10px 0;color: #1C90F6;\">");
		stringBuilder.append(code);
		stringBuilder.append(
				"</span></tr> <tr> <td width=\"720\" height=\"14\" colspan=\"2\" style=\"padding:8px 0 28px;color:#999999;font-size:12px;font-family:'Microsoft YaHei'\">此为系统邮件请勿回复</td>");
		stringBuilder.append(
				"</tr> </tbody> </table> </td> </tr><tr style=\"background-color: #F4F4F4;color: #999999;height: 100px;line-height: 100px;padding-left:20px;font-size: 13px;display:block;\">");
		stringBuilder.append(
				"<td> Copyrights © " +domain+"<span style=\"border-bottom:1px dashed #ccc;z-index:1\" t=\"7\" onclick=\"return false;\" data=\"2019\">2019</span>. All rights reserved. </td> ");
		stringBuilder.append("</tr></tbody></table>");
		MailPOJO mail = new MailPOJO();
		mail.setRecipient(email);
		mail.setSubject("小鼹鼠密码重置邮箱验证");
		mail.setContent(stringBuilder.toString());
		sendHTMLMail(mail);
	}

	@Override
	public void saveMailInfo(EmailInfo emailInfo) {
		emailDao.save(emailInfo);
	}

	@Override
	public boolean checkCode(EmailInfo emailInfo) {
		return emailDao.checkCode(emailInfo) > 0 ? true : false;
	}

}
